package cn.disignPattern.com.partTwo;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Administrator on 2021/6/22 0022.
 */
public class WeatherData implements Subject {

    private Set<Observer> observers;
    private float temp;
    private float humidity;
    private float pressure;

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObserver() {
        observers.stream().forEach( observer -> {
            observer.update(temp,humidity,pressure);
        });


    }

    public void measurementsChanged(){
        notifyObserver();
    }

    public WeatherData() {
        observers = new HashSet<>();
    }

    public void setWeatherData(float temp, float humidity, float pressure) {
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}